//
//  HHPHomeViewController.m
//  HHPLiveVideo
//
//  Created by 韩红坡 on 2017/3/29.
//  Copyright © 2017年 韩红坡. All rights reserved.
//

#import "HHPHomeViewController.h"
#import "TYTabButtonPagerController.h"
#import "HHPHotViewController.h"
#import "HHPCareViewController.h"
#import "HHPMediatorManager+WebView.h"

@interface HHPHomeViewController () <TYPagerControllerDataSource>

@property (nonatomic, strong) TYTabButtonPagerController *pagerController;

@property (nonatomic, strong) NSMutableArray *childViewControllerArray;

@property (nonatomic, strong) NSArray *titleArray;

@end

@implementation HHPHomeViewController
#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"精彩";
    
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    [self addPagerController];
    
    [self.pagerController reloadData];
    
    [self setupNavigationItem];
    
    [self.homeViewModel fetchADDataWithCompletionBlock:^(BOOL success) {
        
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

#pragma mark - TYPagerControllerDataSource
- (NSInteger)numberOfControllersInPagerController
{
    return self.titleArray.count;
}

- (NSString *)pagerController:(TYPagerController *)pagerController titleForIndex:(NSInteger)index
{
    return self.titleArray[index];
}

- (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index
{
    
    return [self.childViewControllerArray objectAtIndex:index];
}

#pragma mark - private methods
- (void)addPagerController
{
    TYTabButtonPagerController *pagerController = [[TYTabButtonPagerController alloc] init];
    pagerController.dataSource = self;
    pagerController.adjustStatusBarHeight = YES;
    pagerController.barStyle = TYPagerBarStyleProgressView;
    pagerController.collectionLayoutEdging = ScreenWidth * 0.1;
    pagerController.cellWidth = ScreenWidth * 0.8 / self.titleArray.count;
    pagerController.cellSpacing = 0;
    pagerController.contentTopEdging = 44;
    pagerController.progressWidth =  44;
    pagerController.progressColor = RGB(240, 240, 240);
    pagerController.progressHeight = 4;
    pagerController.normalTextFont = Font(16);
    pagerController.selectedTextFont = Font(20);
    pagerController.normalTextColor = RGB(220, 220, 220);
    pagerController.selectedTextColor = RGB(240, 240, 240);
    
    pagerController.view.frame = self.view.bounds;
    pagerController.pagerBarColor = [UIColor clearColor];
    pagerController.view.backgroundColor = [UIColor clearColor];

    [self addChildViewController:pagerController];
    [self.view addSubview:pagerController.view];
    
    self.pagerController = pagerController;
}

- (void)setupNavigationItem
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 64)];
    imageView.image = [UIImage imageNamed:@"navBar_bg"];
    [self.pagerController.pagerBarView insertSubview:imageView atIndex:0];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    [leftButton bk_whenTapped:^{
        
    }];
    leftButton.frame = CGRectMake(0, 20, 50, 44);
    [self.pagerController.pagerBarView addSubview:leftButton];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setImage:[UIImage imageNamed:@"head_crown"] forState:UIControlStateNormal];
    [rightButton bk_whenTapped:^{
        [[HHPMediatorManager sharedInstance] mediator_pushWebViewControllerWithAbsoluteUrlString:@"http://live.9158.com/Rank/WeekRank?Random=10" withTitle:@"排行"];
    }];
    rightButton.frame = CGRectMake(self.view.width - 50, 20, 50, 44);
    [self.pagerController.pagerBarView addSubview:rightButton];
}

#pragma mark - getter and setter
- (NSMutableArray *)childViewControllerArray
{
    if (!_childViewControllerArray) {
        _childViewControllerArray = [NSMutableArray array];
        
        HHPHotViewController *hotVC = [[HHPHotViewController alloc] init];
        hotVC.roomType = RoomTypeHot;
        [_childViewControllerArray addObject:hotVC];
        
        HHPHotViewController *artVC = [[HHPHotViewController alloc] init];
        artVC.roomType = RoomTypeArt;
        [_childViewControllerArray addObject:artVC];
        
        HHPCareViewController *careVC = [[HHPCareViewController alloc] init];
        [_childViewControllerArray addObject:careVC];
    }
    
    return _childViewControllerArray;
}

- (NSArray *)titleArray
{
    if (_titleArray == nil) {
        _titleArray = [NSArray arrayWithObjects:@"热门", @"才艺", @"关注", nil];
    }
    return _titleArray;
}

- (HHPHomeViewModel *)homeViewModel
{
    if (!_homeViewModel) {
        _homeViewModel = [[HHPHomeViewModel alloc] init];
    }
    
    return _homeViewModel;
}

@end
